<?php
namespace App\Models\Traits;

use App\Models\SysDept;
use App\Models\SysUser;
use Illuminate\Support\Facades\DB;

trait DataScopeHelper{
    public function scopeDataFilter($query)
    {
        if($this->isFillable('create_by') == false){
            return $query;
        }
        $scope_info = $this->getCreateByIds();
        if($scope_info['scope'] == 'all'){
            return $query;
        }
        if($scope_info['scope'] == 'limit'){
            return $query->whereIn('create_by', $scope_info['ids']);
        }
        return $query->where('create_by', auth()->id());

        $user = SysUser::with('roles')->where('id',auth()->id())->first();
        //获取用户角色
        $user_roles = $user->roles;
        $has_all_data_scope = false;
        $dept_ids = [];
        $user_ids = [];
        $user_id = $user->id;
        if($user_id === 1){
            $has_all_data_scope = true;
            return $query;
        }
        //过滤权限
        foreach ($user_roles as $role){
            // 全部数据权限
            if($role->data_scope == 1){
                $has_all_data_scope = true;
                return $query;
            }
            //自定数据权限
            if($role->data_scope == 2){
                //获取角色部门关联数据
                $role_dept_ids = DB::table('sys_role_dept')->where('role_id', $role->id)->pluck('dept_id');
                if($role_dept_ids){
                    $dept_ids = array_merge($dept_ids, $role_dept_ids->toArray());
                }
            }
            //本部门数据权限
            if($role->data_scope == 3){
                //获取用户所属部门
                $dept_ids = array_merge($dept_ids, [ $user->dept_id ]);
            }
            //本部门及以下数据权限
            if($role->data_scope == 4){
                //获取用户部门以及相关下属部门
                $user_dept_and_child_dept_ids = SysDept::descendantsAndSelf($user->dept_id)->pluck('id');
                if($user_dept_and_child_dept_ids){
                    $dept_ids = array_merge($dept_ids, $user_dept_and_child_dept_ids->toArray());
                }
            }

            //本人数据
            if($role->data_scope == 5){
                $user_ids = array_merge($user_ids, [ $user_id ]);
            }

            //本人数据以及我创建的数据
            if($role->data_scope == 6){
                $tmp_user_ids = SysUser::where('create_by', $user_id)->pluck('id');
                if($tmp_user_ids){
                    $user_ids = array_merge($user_ids, $tmp_user_ids->toArray());
                }
            }

        }
        //部门相关关联数据
        if(count($dept_ids) > 0){
            $user_in_depts_ids = SysUser::whereIn('dept_id', $dept_ids)->pluck('id');
            if($user_in_depts_ids){
                $user_ids = array_merge($user_ids, $user_in_depts_ids->toArray());
            }
        }
        //没有数据集范围 默认只能看自己的
        if(count($user_ids) == 0){
            return $query->where('create_by', $user_id);
        }
        return $query->whereIn('create_by', $user_ids);
    }

    protected function getCreateByIds(){
        $user = SysUser::with('roles')->where('id',auth()->id())->first();
        $user_id = $user->id;
        //获取用户角色
        $user_roles = $user->roles;

        $dept_ids = [];
        $user_ids = [];

        //过滤权限
        foreach ($user_roles as $role){
            // 全部数据权限
            if($role->data_scope == 1){
                $has_all_data_scope = true;
                return ['scope' => 'all'];
            }
            //自定数据权限
            if($role->data_scope == 2){
                //获取角色部门关联数据
                $role_dept_ids = DB::table('sys_role_dept')->where('role_id', $role->id)->pluck('dept_id');
                if($role_dept_ids){
                    $dept_ids = array_merge($dept_ids, $role_dept_ids->toArray());
                }
            }
            //本部门数据权限
            if($role->data_scope == 3){
                //获取用户所属部门
                $dept_ids = array_merge($dept_ids, [ $user->dept_id ]);
            }
            //本部门及以下数据权限
            if($role->data_scope == 4){
                //获取用户部门以及相关下属部门
                $user_dept_and_child_dept_ids = SysDept::descendantsAndSelf($user->dept_id)->pluck('id');
                if($user_dept_and_child_dept_ids){
                    $dept_ids = array_merge($dept_ids, $user_dept_and_child_dept_ids->toArray());
                }
            }

            //本人数据
            if($role->data_scope == 5){
                $user_ids = array_merge($user_ids, [ $user_id ]);
            }

            //本人数据以及我创建的数据
            if($role->data_scope == 6){
                $tmp_user_ids = SysUser::where('create_by', $user_id)->pluck('id');
                if($tmp_user_ids){
                    $user_ids = array_merge($user_ids, $tmp_user_ids->toArray());
                }
            }

        }
        //部门相关关联数据
        if(count($dept_ids) > 0){
            $user_in_depts_ids = SysUser::whereIn('dept_id', $dept_ids)->pluck('id');
            if($user_in_depts_ids){
                $user_ids = array_merge($user_ids, $user_in_depts_ids->toArray());
            }
        }
        //没有数据集范围 默认只能看自己的
        if(count($user_ids) == 0){
            $user_ids = [$user_id];
        }
        return ['scope' => 'limit', 'ids' => $user_ids];
    }
}
